package com.eight.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.TbMember;
import com.eight.teacher.dao.TbMemberDao;
import com.eight.teacher.dao.TeacherDao;
import com.eight.common.pojo.EduTeacher;
import com.eight.teacher.vo.EduTeacherVo;
import com.eight.teacher.service.TeacherService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @program: qy8-education
 * @description: TeacherServiceImpl
 * @author: 韩宗耀
 * @create: 2023-05-27 15:12
 **/
@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherDao teacherDao;

    @Resource
    private TbMemberDao tbMemberDao;

    @Override
    public Page<EduTeacher> findTeacher(Integer pageCurrent, Integer pageSize, EduTeacherVo eduTeacherVo) {
        Page<EduTeacher> page = new Page<>(pageCurrent, pageSize);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(eduTeacherVo.getName())) {
            wrapper.like("name",eduTeacherVo.getName());
        }
        if (StringUtils.hasText(eduTeacherVo.getMobile())) {
            wrapper.like("mobile",eduTeacherVo.getMobile());
        }
        if (StringUtils.hasText(eduTeacherVo.getStatus())) {
            wrapper.like("status",eduTeacherVo.getStatus());
        }
        return teacherDao.selectPage(page,wrapper);
    }

    @Override
    public Integer updateTeacher(EduTeacher eduTeacher) {
        eduTeacher.setGmtModified(LocalDateTime.now());
        return teacherDao.updateById(eduTeacher);
    }

    @Override
    public Integer changeStatus(EduTeacher eduTeacher) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("id",eduTeacher.getId());
        // 传过来的有id，根据id查到手机号
        EduTeacher teacher = teacherDao.selectOne(wrapper);
        // 修改讲师表的讲师状态
        eduTeacher.setGmtModified(LocalDateTime.now());
        teacherDao.update(eduTeacher,wrapper);
        // 修改会员表的用户类型
        QueryWrapper<TbMember> wrapper1 = new QueryWrapper<>();
        TbMember tbMember = new TbMember();
        tbMember.setGmtModified(LocalDateTime.now());
        if (eduTeacher.getStatus().equals("1")){
            tbMember.setIsType(0);
        }
        if (eduTeacher.getStatus().equals("0")){
            tbMember.setIsType(1);
        }
        wrapper1.eq("mobile",teacher.getMobile());
        tbMemberDao.update(tbMember,wrapper1);

        return null;
    }
}
